package com.pug.mail.utils;

import com.pug.mail.bo.EmailAttachmentBO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;


/**
 * @author RenGaoshuai
 * @date 2023/6/29 9:59
 * @description 邮箱发送工具
 */
@Component
@Slf4j
public class EmailSendUtil {

    private static JavaMailSenderImpl mailSender;

    /**
     * set 方式注入 JavaMailSenderImpl
     * @param mailSender
     */
    @Autowired
    public void setMailSender(JavaMailSenderImpl mailSender) {
        EmailSendUtil.mailSender = mailSender;
    }



    /**
     * 发送邮件
     *
     * @param subject 邮件标题
     * @param to      发送地址
     * @param text    邮件内容
     * @return 是否发送成功
     */
    public static boolean sendMail(String subject, String to, String text) {
        boolean isSuccess = true;

        //标准消息类
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

            helper.setFrom(mailSender.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

            //发送邮件
            mailSender.send(mimeMessage);

        } catch (Exception e) {
            log.error("发送邮件错误 sendMail: subject [{}], to [{}], text [{}]", subject, to, text, e);
            isSuccess = false;
        }

        return isSuccess;
    }


    /**
     *发送邮件
     * @param subject 邮件标题
     * @param to      发送地址
     * @param text    邮件内容
     * @param attachment 附件
     * @return 是否发送成功
     */
    public static boolean sendMail(String subject, String to, String text, EmailAttachmentBO attachment){
        boolean isSuccess = true;

        //标准消息类
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

            helper.setFrom(mailSender.getUsername());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

            //附件处理
            String attachmentName = attachment.getAttachmentName();
            byte[] attachmentData = attachment.getAttachmentData();

            String encodeWord = MimeUtility.encodeWord(attachmentName, "utf-8", "B");
            ByteArrayResource byteArrayResource = new ByteArrayResource(attachmentData);

            helper.addAttachment(encodeWord,byteArrayResource);

            //发送邮件
            mailSender.send(mimeMessage);

        } catch (Exception e) {
            log.error("发送邮件错误 sendMail: subject [{}], to [{}], text [{}]", subject, to, text, e);
            isSuccess = false;
        }

        return isSuccess;
    }



}
